<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------

namespace Help\Model;

use Think\Model;

/**
 * Class HelpModel  帮忙模型
 * @package Help\Model
 * @author:xjw129xjt xjt@ourstu.com
 */
class HelpModel extends Model
{

    protected $tableName = 'help';

    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', '1', self::MODEL_INSERT),
    );

    protected $insertFields = 'title,province,city,district,s_time,e_time,s_height,e_height,s_age,e_age,type,sex,number,description,cover_id,user_rate'; // 新增数据的时候允许写入name和email字段
    protected $updateFields = 'title,province,city,district,s_time,e_time,s_height,e_height,s_age,e_age,type,sex,number,description,cover_id,user_rate'; // 编辑数据的时候只允许写入email字段

    public function addHelp($data)
    {
        $data = $this->create($data);
        $data['uid'] = is_login();
        $data['state'] = 1;
        $res = $this->add($data);
        return $res;
    }

    public function editHelp($data)
    {
        $id = $data['id'];
        $data = $this->create($data);
        $data['state'] = 1;
        $res = $this->where(array('id' => $id))->save($data);
        S('help_' . $id, null);
        return $res;
    }

    public function deleteHelp($id)
    {
        $res = $this->where(array('id' => $id))->setField('status', 0);
        S('help_' . $id, null);
        return $res;
    }

    public function getHelp($id)
    {
        $help = S('help_' . $id);
        if (empty($help)) {
            $help = $this->where(array('id' => $id, 'status' => 1))->find();

/*            $e_count = D('HelpEvaluate')->getEvaluateCount(array('help_id' => $id,'to_uid'=>$help['uid']));
            !empty($help) && $help['rate'] =  number_format($help['good_count']/$e_count*100,0);*/
            if(!empty($help)){
                $rate=query_user(array('rate'),$help['uid']);
                $rate=$rate['rate'];
                $help['rate'] = $rate;

                $help['help_type'] = type_title($help['type']);
                if (mb_strlen(op_t($help['description']), 'utf-8') < 50) {
                    if(op_t($help['description'])==$help['description']){
                        $help['content_s'] = $help['description'];
                    }else{
                        $help['content_s'] = op_t($help['description']).'...';
                    }
                } else {
                    $help['content_s'] = msubstr(op_t($help['description']), 0, 50);
                }
                S('help_' . $id, $help);
            }
        }
        !empty($help) && $help['user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature','credibility','address','doing','fans'), $help['uid']);
        $time = time();
        if ($help['state'] != 0 && $help['state'] != 4) {
            if (intval($help['s_time']) >= $time + modC('SIGN_DEADLINE') * 24 * 60 * 60 && $help['state'] != 1) {
                D('Help')->where(array('id' => $id))->setField('state', 1);
                S('help_' . $id, null);
                $help['state'] = 1;
            } elseif (intval($help['s_time']) >= $time + modC('CHOSE_DEADLINE') * 24 * 60 * 60 && $help['state'] != 2 && check_sign_is_full($id)) {
                D('Help')->where(array('id' => $id))->setField('state', 2);
                S('help_' . $id, null);
                $help['state'] = 2;
            } elseif (intval($help['s_time']) < $time + modC('SIGN_DEADLINE') * 24 * 60 * 60 && $help['state'] != 4 && !check_sign_is_full($id)) {
                D('Help')->where(array('id' => $id))->setField('state', 4);
                S('help_' . $id, null);
                $help['state'] = 4;
            } elseif (intval($help['s_time']) < $time + modC('CHOSE_DEADLINE') * 24 * 60 * 60 && $help['state'] != 0 && !check_publisher_is_chose($id)) {
                D('Help')->where(array('id' => $id))->setField('state', 0);
                S('help_' . $id, null);
                $help['state'] = 0;
            } elseif ($time > intval($help['s_time']) - modC('CHOSE_DEADLINE') * 24 * 60 * 60 && $time < intval($help['e_time']) && $help['state'] != 3 && check_publisher_is_chose($id)) {
                D('Help')->where(array('id' => $id))->setField('state', 3);
                S('help_' . $id, null);
                $help['state'] = 3;
            } elseif ($time > intval($help['e_time']) && $help['state'] != 5 && !check_is_all_evaluate($id)) {
                D('Help')->where(array('id' => $id))->setField('state', 5);
                S('help_' . $id, null);
                $help['state'] = 5;
            } elseif ($time > intval($help['e_time']) && $help['state'] != 6 && check_is_all_evaluate($id)) {
                D('Help')->where(array('id' => $id))->setField('state', 6);
                S('help_' . $id, null);
                $help['state'] = 6;
            } elseif ($time > intval($help['e_time']) && $help['state'] != 7 && check_is_all_evaluate($id) && check_is_publish_work($id)) {
                D('Help')->where(array('id' => $id))->setField('state', 7);
                S('help_' . $id, null);
                $help['state'] = 7;
            }
        }
        return $help;
    }

    public function getList($map = '', $order = 'id desc')
    {
        $help_list = $this->where($map)->order($order)->field('id')->select();
        $ids = getSubByKey($help_list, 'id');
        $list = array();
        foreach ($ids as $v) {
            $list[] = $this->getHelp($v);
        }
        unset($v);
        return $list;
    }

    public function getPage($map = '',&$totalCount,$page = 1, $r = 50, $order = 'id desc')
    {

        $help_list = $this->where($map)->order($order)->field('id')->page($page, $r)->select();
        $totalCount=$this->where($map)->count();
        $ids = getSubByKey($help_list, 'id');
        $list = array();
        foreach ($ids as $v) {
            $list[] = $this->getHelp($v);
        }
        unset($v);
        return $list;
    }

    //zzl start
    public function getIds($map='',&$totalCount,$order='id desc'){
        $help_list = $this->where($map)->order($order)->field('id')->select();
        $totalCount=$this->where($map)->count();
        $ids = getSubByKey($help_list,'id');
        return $ids;
    }

    /**通过用户id获取发布的帮忙
     * @param array $uids
     * @param int $page
     * @param $totalCount
     * @param array $recommendIds
     * @return array
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function getHelpListByUids($uids=array(),$page=1,&$totalCount,$recommendIds=array()){
        if(is_array($recommendIds)&&count($recommendIds)){
            $helpIds=$this->getIds(array('id'=>array('not in',$recommendIds),'uid'=>array('in',$uids)),$count);//排除推荐的帮忙，的id
            $totalCount=$count+count($recommendIds);
            if(is_array($helpIds)){
                $recommendIds=array_merge($recommendIds,$helpIds);
            }
            $helpIds=array_slice($recommendIds,($page-1)*50,50);
            $helpList = array();
            foreach($helpIds as $v){
                $helpList[] =  $this->getHelp($v);
            }
            unset($v);
        }else{
            $helpList=$this->getPage(array('uid'=>array('in',$uids)),$totalCount,$page,50);
        }
        return $helpList;
    }

    /**获取用户发布或参与的帮忙封面
     * @param int $uid
     * @param int $limit 获取数量
     * @return array
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function getHelpCover($uid=0,$limit=3){
        $map['uid']=$uid;
        $map['status']=1;
        $myHelpIds = $this->where($map)->limit($limit)->field('id')->select();
        $myHelpIds=array_column($myHelpIds,'id');
        if(count($myHelpIds)<$limit){
            $otherHelpIds=D('HelpAttend')->where(array('uid'=>$uid,'status'=>1))->field('help_id')->limit($limit-count($myHelpIds))->select();
            $otherHelpIds=array_column($otherHelpIds,'help_id');
            if(count($otherHelpIds)){
                $map_ot['id']=array('in',$otherHelpIds);
                $map_ot['status']=1;
                $otherHelpIds=$this->where($map_ot)->limit($limit)->field('id')->select();
                $otherHelpIds=array_column($otherHelpIds,'id');
            }
        }

        $helpIds=array();
        count($myHelpIds)&&$helpIds=$myHelpIds;
        count($otherHelpIds)&&$helpIds=array_merge($helpIds,$otherHelpIds);
        $helpList=array();
        foreach($helpIds as $v){
            $helpList[]=$this->getHelp($v);
        }
        unset($v);
        return $helpList;
    }

    public function getUidById($id){
        $uid=$this->field('uid')->find($id);
        return $uid['uid'];
    }
}